package com.psp.web.persist.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.stereotype.Repository;

import com.psp.web.model.AddressBean;
import com.psp.web.persist.dao.AddressDao;

@Repository
public class AddressImpl extends BaseImpl implements AddressDao {
	protected static final String NAME_SPACE = NAME_SPACE_HEADER + ".AddressMapper";

	@Override
	public AddressBean selectById(String aid) {
		return sqlSessionTemplate.selectOne(NAME_SPACE + ".selectById", aid);
	}

	@Override
	public List<AddressBean> selectByIds(Set<String> aids) {
		Map<String, Object> where = new HashMap<String, Object>();
		where.put("aids", aids);
		return sqlSessionTemplate.selectList(NAME_SPACE + ".selectByIds", where);
	}

	@Override
	public int insert(AddressBean bean) {
		return sqlSessionTemplate.insert(NAME_SPACE + ".insert", bean);
	}

	@Override
	public int update(AddressBean bean) {
		return sqlSessionTemplate.update(NAME_SPACE + ".update", bean);
	}

	@Override
	public int selectCount(String uid, int type) {
		Map<String, Object> where = new HashMap<>();
		where.put("uid", uid);
		where.put("type", type);
		return sqlSessionTemplate.selectOne(NAME_SPACE + ".selectCount", where);
	}

	@Override
	public List<AddressBean> selectList(int page, int pageSize, String uid, int type) {
		Map<String, Object> where = new HashMap<>();
		where.put("start", page * pageSize);
		where.put("length", pageSize);
		where.put("uid", uid);
		where.put("type", type);
		return sqlSessionTemplate.selectList(NAME_SPACE + ".selectList", where);
	}

	@Override
	public int deleteById(String aid, String uid) {
		Map<String, Object> where = new HashMap<>();
		where.put("aid", aid);
		where.put("uid", uid);
		return sqlSessionTemplate.delete(NAME_SPACE + ".deleteById", where);
	}

	@Override
	public int updateDefault(String uid, Integer type, Integer isDefault) {
		Map<String, Object> where = new HashMap<>();
		where.put("uid", uid);
		where.put("type", type);
		where.put("isDefault", isDefault);
		return sqlSessionTemplate.update(NAME_SPACE + ".updateDefault", where);
	}

	@Override
	public AddressBean selectDefault(String uid, Integer isDefault) {
		Map<String, Object> where = new HashMap<>();
		where.put("uid", uid);
		where.put("isDefault", isDefault);
		return sqlSessionTemplate.selectOne(NAME_SPACE + ".selectDefault", where);
	}

}
